﻿using System.Collections.Generic;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using Abp.Application.Services.Dto;
using Abp.Extensions;
using Abp.Linq.Extensions;
using Abp.UI;
using DHJ.Cost.Tasks.Dto;
using Microsoft.EntityFrameworkCore;

namespace DHJ.Cost.Tasks
{
    public class BatchAppService : CostDomainServiceBase, IBatchAppService
    {
        private readonly IBatchManager _batchManager;

        public BatchAppService(IBatchManager batchManager)
        {
            _batchManager = batchManager;
        }

        public async Task<PagedResultDto<BatchDto>> GetBatches(GetBatchesInput input)
        {
            var query = _batchManager.Batches().Include(e => e.Task)
                .WhereIf(!input.Filter.IsNullOrWhiteSpace(),
                    e => e.Task.ProductId.Contains(input.Filter) ||
                         e.Id.Contains(input.Filter) ||
                         e.Certificate.Contains(input.Filter) ||
                         e.QcCard.Contains(input.Filter) ||
                         e.Task.TaskNum.Contains(input.Filter));

            var count = await query.CountAsync();

            var batches = await query
                .OrderBy(input.Sorting)
                .PageBy(input)
                .ToListAsync();

            return new PagedResultDto<BatchDto>(
                count,
                ObjectMapper.Map<List<BatchDto>>(batches)
            );
        }

        public async Task<BatchDto> GetBatchById(string batchId)
        {
            var batch = await _batchManager.Batches().Include(e => e.Task).FirstOrDefaultAsync(e => batchId == e.Id); ;
            if (batch == null) throw new UserFriendlyException("不存在该条码，请检查输入是否正确。");
            return ObjectMapper.Map<BatchDto>(batch);
        }

        public async Task<ListResultDto<AssemRelListDto>> GetAssemRels(string batchId)
        {
            var result = await _batchManager.GetAssemRelsAsync(batchId);

            var list = ObjectMapper.Map<List<AssemRelListDto>>(result);

            foreach (var item in list)
            {
                var batch1 = await _batchManager.Batches().Include(e => e.Task).FirstOrDefaultAsync(e => item.PartId == e.Id);

                if (batch1 != null)
                {
                    item.TaskNum = batch1.Task?.TaskNum;
                    item.ProductId = batch1.Task?.ProductId;
                    item.ProductName = batch1.Task?.ProductName;
                }
            }

            return new ListResultDto<AssemRelListDto>(list);
        }

        public async Task<ListResultDto<AssemRelListDto>> GetFlatAssemRels(BatchQueryDto batch)
        {
            var result = await _batchManager.GetFlatAssemRelsAsync(new BatchIdentifier(batch.Id, batch.Quantity));

            var list = ObjectMapper.Map<List<AssemRelListDto>>(result);

            foreach (var item in list)
            {
                var batch1 = await _batchManager.Batches().Include(e => e.Task).FirstOrDefaultAsync(e => item.PartId == e.Id);

                if (batch1 != null)
                {
                    item.TaskNum = batch1.Task?.TaskNum;
                    item.ProductId = batch1.Task?.ProductId;
                    item.ProductName = batch1.Task?.ProductName;
                }
            }

            return new ListResultDto<AssemRelListDto>(list);
        }


  
    }
}